<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

quanli_require('module/base/system/itemHandler.class.php');

/**
 * 后台退换货控制器
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 */

quanli_require('module/base/system/sysRegion.class.php');
quanli_require('module/base/system/sysDistribution.class.php');
quanli_require('module/base/system/sysItemMultilevel.class.php');
class pa_billReturn {

    private $is_AL = 0; //澳粒退货单标识

    public function execute() {

        if(isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0 ) {

            switch ($_REQUEST['operFlg']) {

                case 1://订单退货查询
                    $this->billReturnSearch();
                    break;

                case 2://订单退货
                case 4://订单拒收
                    $this->billReturn();
                    break;

                case 5://查询退换货单详情
                    $this->billReturnDetail();
                    break;

                case 6: //澳粒退货单查询
                    $this->AL_billReturnSearch();
                    break;

                default:
                    backendError();
                    break;

            }
        } else {

            backendError();

        }

    }


    /**
     * 拒收/退货列表
     */
    private function billReturnSearch() {

        global $G_PGLOB;

        //实例化配送网点设置类
        $sysDistribution = new sysDistribution( ) ;

        $arg_array = $_REQUEST;

        $return_array = $sysDistribution->search( $arg_array ) ;

        $PS_LOGIN_SESSION =$G_PGLOB->sessionCache_get('PS_LOGIN_SESSION');
        $distribution_array = array();
        if($return_array){
            foreach($return_array as $distribution){
                if(in_array($distribution['distribution_id'], $PS_LOGIN_SESSION['distribution'])){
                    $distribution_array[] = $distribution;
                }
            }
        }

        //是澳粒退货单
        if($this->is_AL == 1){
            $billPlatform = 2;
            $G_PGLOB->smarty->assign_by_ref('billPlatform', $billPlatform);
        }
        /*
         * add by allen.qiang allen.qiang@fistforward.com 2015年2月27日 退货单查询界面保存布局 FUN-BK-0048 start
         */

            $table_id=8;
            $flex_array = array(
                array('display' => '操作','width' => 200,'align' => 'center'),
                array('display' => '状态','name' => 'bill_no', 'width' => 260, 'sortable' => true, 'align' => 'center'),
                array('display' => '退货单编号','name' => 'billType', 'width' => 100, 'sortable' => true),
                array('display' => '订单编号','width' => 80),
                array('display' => '退货商品名称','width' => 80),
                array('display' => '订单平台','width' => 120),
                array('display' => '配送时间','width' => 200),
                array('display' => '会员名','width' => 200),
                array('display' => '联系人','width' => 140,'align' => 'center'),
                array('display' => '手机号码','width' => 300),
                array('display' => '退货原因','width' => 80),
                //add by sun.qiang  2015-03-25 MK-FUN-BK-004 新增品控意见 --- start
                array('display' => '责任部门','width' => 90),
                array('display' => '品控意见','width' => 150),
                //add by sun.qiang  2015-03-25 MK-FUN-BK-004 新增品控意见 --- end
                array('display' => '配送站','width' => 80),
                array('display' => '创建人','width' => 80),
                array('display' => '创建时间','width' => 80),
                array('display' => '备注','width' => 80),
                array('display' => '审核人','width' => 80),
                array('display' => '审核时间','width' => 80),
                array('display' => '收货人','width' => 80),
                array('display' => '收货时间','width' => 100),
                array('display' => '取消人','width' => 60),
                array('display' => '取消时间','width' => 80),
                array('display' => '自购操作人','width' => 80),
                array('display' => '自购操作时间', 'width' => 120),

            ); //flexigrid插件表格  end

            quanli_require('module/base/system/sysFlexigridLog.class.php');

            $sysFlexigridLog = new sysFlexigridLog();

            $arg_array = array(
                'table_id' => $table_id,
                'userID' => $G_PGLOB->currentUserID,
                'sortname' => 'last_cid',
                'sortorder' => 'ASC'
            );
            $flexigrid_array = $sysFlexigridLog->searchFlexigridInfo($arg_array);

        if($flexigrid_array && $this->is_AL != 1){
            $flex_array = $flexigrid_array;
            }else{
                $arg_array = array(
                    'table_id' => $table_id,
                    'userID' => $G_PGLOB->currentUserID,
                    'data' => $flex_array
                );
                $sysFlexigridLog->editFlexigrid($arg_array);
            }
            $G_PGLOB->smarty->assign_by_ref('table_id', $table_id);
            $G_PGLOB->smarty->assign('is_AL', $this->is_AL);
            $G_PGLOB->smarty->assign_by_ref('fixed_json', json_encode($flex_array));





        /*
         * add by allen.qiang allen.qiang@fistforward.com 2015年2月27日 退货单查询界面保存布局 FUN-BK-0048 end
         */




        //add by sun.qiang  2015-03-25 MK-FUN-BK-004 新增品控意见 --- start
        $sysItemMultilevel = new sysItemMultilevel($G_PGLOB->DBCA);
        $arg_array = array(
            'parent_id'=>26
        );
        $qcviews_items = $sysItemMultilevel->searchMidRegion($arg_array);
//        $qc_select_options = '';
//        foreach($qcviews_items as $item)
//        {
//            $qc_select_options .= " <option value=\"".$item['item_id']."\">".$item['item_name']."</option> ";
//        }
        $G_PGLOB->smarty->assign_by_ref('qcviews_items',$qcviews_items);

        //add by sun.qiang  2015-03-25 MK-FUN-BK-004 新增品控意见 --- end

        if (isset($_REQUEST['kf_return_no']) && quanli_is_int($_REQUEST['kf_return_no'])) {
            $G_PGLOB->smarty->assign('kf_return_no', $_REQUEST['kf_return_no']);
        }
        $G_PGLOB->smarty->assign_by_ref('distribution_array', $distribution_array);

        $G_PGLOB->smarty->display('bill/billReturn.tpl');

    }

    /**
     * 澳粒拒收/退货列表
     */
    
    private function AL_billReturnSearch(){

        $this->is_AL = 1;

        $this->billReturnSearch();

    }


    /**
     * 订单拒收、退换货
     */
    private function billReturn() {

        if (!isset($_REQUEST['bill_no']) || empty($_REQUEST['bill_no'])){
            backendError();
        }

        global $G_PGLOB;

        //加载类文件
        quanli_include('module/base/bill/billSearch.class.php');
        //加载类文件
        quanli_include('module/base/bill/billReturn.class.php');

        //实例订单类
        $billSearch = new billSearch();

        //订单号
        $bill_no = $_REQUEST['bill_no'];

        //查询订单信息
        $arg_array = array(
            'bill_no' => $bill_no
        );
        $bill_array = $billSearch->searchBillInfo( $arg_array );

        //检查订单状态
        if (!$bill_array) {
            backendError('订单信息没有找到');
        }
        if (in_array($bill_array[0]['bill_status'],array(0, 1, 2))) {
            backendError('订单未出库无法创建退货单，您可以修改或取消订单');
        }
        if ( $bill_array[0]['bill_status'] == 10 ) {
            backendError('订单已取消无法创建退货单');
        }

        //查询订单商品信息
        $arg_array = array(
            'bill_no' => $bill_no
        );
        $bill_goods_array = $billSearch->searchBillGoods( $arg_array );
        if (!$bill_goods_array) {
            backendError('订单商品信息没有找到');
        }


        //查询支付信息
        //mod by 20141028 sunqiang FUN-BK-0012 退货单默认退货金额 start
        $billSearch = new billSearch( $G_PGLOB->DBCA );
        //$arg_array = array(
        //    'bill_no' => $_REQUEST['bill_no']
        //);
        $arg_array = array(
            'bill_no' => $_REQUEST['bill_no'],
            'sortname' => 'type',
            'sortorder' => 'asc'
        );
        //mod by 20141028 sunqiang FUN-BK-0012 退货单默认退货金额 end
        $bill_pay_array = $billSearch->searchBillPay( $arg_array );

        //实例退货类
        $billReturn = new billReturn( $G_PGLOB->DBCA );

        $arg_array = array (
            'bill_no'=> $_REQUEST['bill_no']
        );
        $bill_return_array = $billReturn->searchBillReturn( $arg_array );

        $return_pay_array = array(); //保存已退货的支付方式
        $return_card_array = array(); //保存已退货的现金卡
        $return_coupon_array = array(); //保存已退货的优惠券
        $identify = 0; //货到付款标识
        if($bill_return_array){
            foreach($bill_return_array as $bill_return){
                if($bill_return['return_status'] != 4){
                    $arg_array = array (
                        'return_no'=> $bill_return['return_no']
                    );
                    $bill_return_pay_array = $billReturn->searchBillReturnPay( $arg_array );

                    foreach($bill_return_pay_array as $return){
                        $return_pay_array[] = $return['payTypeName'];
                    }
                }
            }
        }

        if($bill_return_array){
            foreach($bill_return_array as $bill_return_coupon){
                if($bill_return_coupon['return_status'] != 4){
                    $return_sql = "SELECT * FROM bill_return_coupon_card WHERE return_no = ".$bill_return_coupon['return_no'];
                    $return_array = $G_PGLOB->DBCA->stmt_query($return_sql);

                    if($return_array){
                        foreach($return_array as $return){
                            $return_card_array[] = $return->card_no;
                            $return_coupon_array[] = $return->coupon_code;
                        }
                    }
                }
            }
        }

        if($bill_pay_array){
            $i = 0;
            foreach($bill_pay_array as $key => $bill_pay){
                if($bill_pay['type'] == 1 || $bill_pay['type'] == 2){
                    $identify = 1;
                }
                if($bill_pay['type'] == 6){
                    $bill_coupon_array = $billSearch->searchBillUsedCoupon($bill_pay['bill_no']);

                    if($bill_coupon_array){
                        foreach($bill_coupon_array as $bill_coupon){
                            $bill_pay_array[$key]['coupon'][$i]['coupon_code'] = $bill_coupon['code'];
                            $bill_pay_array[$key]['coupon'][$i]['coupon_name'] = $bill_coupon['couponName'];
                            $bill_pay_array[$key]['coupon'][$i]['coupon_type'] = $bill_coupon['couponType'];
                            $bill_pay_array[$key]['coupon'][$i]['amount'] = $bill_coupon['amount'];
                            $i ++;
                        }
                    }
                }

                if($bill_pay['type'] == 7){
                    $k = 0;
                    $bill_card_array = $billSearch->searchBillUsedCard($bill_pay['bill_no']);
                    if($bill_card_array){
                        foreach($bill_card_array as $bill_card){
                            $bill_pay_array[$key]['card'][$k]['card_no'] = $bill_card['card_no'];
                            $bill_pay_array[$key]['card'][$k]['card_name'] = $bill_card['card_name'];
                            $bill_pay_array[$key]['card'][$k]['face_value'] = $bill_card['face_value'];
                            $bill_pay_array[$key]['card'][$k]['deduction'] = $bill_card['deduction'];
                            $k ++;
                        }
                    }
                }
            }
        }


        //add by 20141028 sunqiang FUN-BK-0012 退货单默认退货金额 start
        $use_amount_total = 0 ;


        foreach($bill_goods_array as $goods)
        {
            $use_amount_total += ( $goods['goods_amount'] - $goods['goods_withdrawAmount'] - $goods['goods_rejectionAmount'] );
        }

        $G_PGLOB->smarty->assign_by_ref('bill_goods_array_json', json_encode( $bill_goods_array ) );

        $G_PGLOB->smarty->assign_by_ref('use_amount_total', $use_amount_total);
        //add by 20141028 sunqiang FUN-BK-0012 退货单默认退货金额 end



        $G_PGLOB->smarty->assign_by_ref('bill_array', $bill_array[0]);
        $G_PGLOB->smarty->assign_by_ref('bill_array_json', json_encode($bill_array[0]));
        $G_PGLOB->smarty->assign_by_ref('bill_goods_array', $bill_goods_array);
        $G_PGLOB->smarty->assign_by_ref('bill_pay_array', $bill_pay_array);
        $G_PGLOB->smarty->assign_by_ref('return_pay_array', $return_pay_array);
        $G_PGLOB->smarty->assign_by_ref('return_card_array', $return_card_array);
        $G_PGLOB->smarty->assign_by_ref('return_coupon_array', $return_coupon_array);
        $G_PGLOB->smarty->assign_by_ref('identify', $identify);

        $G_PGLOB->smarty->display('bill/billReturnAdd.tpl');

    }


    /**
     * 退货/拒收单详情
     */
    private function billReturnDetail() {

        global $G_PGLOB;

        if(!isset($_REQUEST['return_no']) || empty($_REQUEST['return_no']) ){
            backendError('参数错误');
        }

        quanli_include('module/base/bill/billReturn.class.php');

        //实例退货类
        $billReturn = new billReturn( $G_PGLOB->DBCA );

        $arg_array = array(
            'return_no' => $_REQUEST['return_no']
        );

        //服务单主表信息
        $bill_return_array = $billReturn->searchBillReturn( $arg_array );

        //退货商品信息
        $return_goods_array = $billReturn->searchBillReturnGoods( $arg_array );

        $G_PGLOB->smarty->assign_by_ref('bill_return_array', $bill_return_array);

        $G_PGLOB->smarty->assign_by_ref('return_goods_array', $return_goods_array);

        $G_PGLOB->smarty->display('bill/billReturnDetail.tpl');

    }


}
